import createAxios from '/@/utils/axios'
import { useSiteConfig } from '/@/stores/siteConfig'
import { ElNotification, UploadRawFile } from 'element-plus'
import { randomNum, shortUuid } from '/@/utils/random'
import { fullUrl } from '/@/utils/common'
import { isAdminApp } from '/@/utils/common'
import COS from 'cos-js-sdk-v5'

export const state = () => {
    const siteConfig = useSiteConfig()
    return siteConfig.upload.mode == 'local' ? 'disable' : 'enable'
}

export async function fileUpload(fd: FormData, params: anyObj = {}) {
    const siteConfig = useSiteConfig()
    if (typeof(siteConfig.upload.params.error) === 'number') {
        console.log(typeof(siteConfig.upload.params.error))
        ElNotification({
            type: 'error',
            title: siteConfig.upload.params.code,
            message: siteConfig.upload.params.message,
        })
        return false
    }
    const file = fd.get('file') as UploadRawFile
    const sha1 = await getFileSha1(file)
    const fileKey = getSaveName(file, sha1)
    delete params.uuid

    return new Promise(async (resolve, reject) => {
        const cos = new COS({
            getAuthorization: function (options, callback) {
                createAxios({
                    url: isAdminApp() ? '/admin/Cos/refreshToken' : '/api/Cos/refreshToken',
                    method: 'get'
                }).then((res: any) => {
                    callback({
                        TmpSecretId: res.data.tmpSecretId,
                        TmpSecretKey: res.data.tmpSecretKey,
                        SecurityToken: res.data.sessionToken,
                        StartTime: res.data.startTime,
                        ExpiredTime: res.data.expiredTime,
                    });
                })
            }
        })
        cos.uploadFile({
            Bucket: siteConfig.upload.params.bucket,
            Region: siteConfig.upload.params.region,
            Key: fileKey,
            Body: file,
            SliceSize: 1024 * 1024 * 5,
            onFileFinish: function (err, data, options) {   /* 非必须 */
                const fileUrl = '/' + options.Key
                createAxios({
                    url: isAdminApp() ? '/admin/Cos/callback' : '/api/Cos/callback',
                    method: 'POST',
                    data: {
                        url: fileUrl,
                        name: file.name,
                        size: file.size,
                        type: file.type,
                        sha1: sha1,
                    }
                })
                resolve({
                    code: 1,
                    data: {
                        file: {
                            full_url: fullUrl(fileUrl),
                            url: fileUrl,
                        },
                    },
                    msg: '',
                    time: Date.now(),
                })
            },
        });

        // createAxios(
        //     {
        //         url: siteConfig.upload.params.partUrl,
        //         method: 'POST',
        //         data: formData,
        //         params: params,
        //     },
        //     {
        //         showCodeMessage: false,
        //     }
        // ).catch((res) => {
        //     console.log(res)
        //     return false
        //     // 七牛的code不会等于1，失败或成功都会被axios封装拦截到此处
        //     if (res.code && res.error) {
        //         ElNotification({
        //             type: 'error',
        //             message: res.error,
        //         })
        //         reject({
        //             code: 0,
        //             data: res,
        //             msg: res.error,
        //             time: Date.now(),
        //         })
        //     } else {
        //         const fileUrl = '/' + res.key
        //         createAxios({
        //             url: isAdminApp() ? '/admin/Qiniu/callback' : '/api/Qiniu/callback',
        //             method: 'POST',
        //             data: {
        //                 url: fileUrl,
        //                 name: file.name,
        //                 size: file.size,
        //                 type: file.type,
        //                 sha1: sha1,
        //             },
        //         })
        //         resolve({
        //             code: 1,
        //             data: {
        //                 file: {
        //                     full_url: fullUrl(fileUrl),
        //                     url: fileUrl,
        //                 },
        //             },
        //             msg: res.error,
        //             time: Date.now(),
        //         })
        //     }
        // })
    }) as ApiPromise
}

export function getSaveName(file: UploadRawFile, sha1: string) {
    const fileSuffix = file.name.substring(file.name.lastIndexOf('.') + 1)
    const fileName = file.name.substring(0, file.name.lastIndexOf('.'))
    const dateObj = new Date()

    const replaceArr: anyObj = {
        '{topic}': 'default',
        '{year}': dateObj.getFullYear(),
        '{mon}': ('0'+(dateObj.getMonth()+1)).slice(-2),
        '{day}': dateObj.getDate(),
        '{hour}': dateObj.getHours(),
        '{min}': dateObj.getMinutes(),
        '{sec}': dateObj.getSeconds(),
        '{random}': shortUuid(),
        '{random32}': randomNum(32, 32),
        '{filename}': fileName.substring(0, 100),
        '{suffix}': fileSuffix,
        '{.suffix}': '.' + fileSuffix,
        '{filesha1}': sha1,
    }
    const replaceKeys = Object.keys(replaceArr).join('|')
    const siteConfig = useSiteConfig()

    const savename = siteConfig.upload.savename[0] == '/' ? siteConfig.upload.savename.slice(1) : siteConfig.upload.savename

    return savename.replace(new RegExp(replaceKeys, 'gm'), (match) => {
        return replaceArr[match]
    })
}

async function getFileSha1(file: UploadRawFile) {
    const hashBuffer = await crypto.subtle.digest('SHA-1', await file.arrayBuffer())
    const hashArray = Array.from(new Uint8Array(hashBuffer))
    const hashHex = hashArray.map((b) => b.toString(16).padStart(2, '0')).join('')
    return hashHex
}
